import { apiBaseUrl } from '@/config/config.js';
import * as common from '@/config/common.js'
import * as helper from '@/utils/index.js'
import * as auth from '@/utils/auth.js'

// 需要登陆的，都写到这里，否则就是不需要登陆的接口
const methodsToken = [
	"api/UserCheckInfo/UserClockIn",
	"api/UserCheckInfo/UserClockOut",
	"api/UserCheckInfo/GetUserCheckInfoByDay",
	"api/ProjectInfo/Create",
	"api/ProjectInfo/Update",
	"api/ProjectInfo/Get",
	"api/ProjectItemInfo/GetAllByProjectId",
	"api/CostType/Create",
	"api/CostType/Update",
	"api/CostType/GetAll",
	"api/CostApply/GetMyCostApplyList",
	"api/CostApply/GetCostApplyById",
	"api/CostApply/Create",
	"api/ProjectItemImages/GetAllImagesByItemId",
	"api/ProjectItemImages/InsertMany",
	"api/WorkerTypeInfo/QueryAll",
	"api/UserCheckDetail/Add",
	"api/UserCheckDetail/Update",
	"api/UserCheckDetail/Delete",
	"api/UserCheckDetail/QueryAll",
	"api/BuildingSiteBill/Add",
	"api/Upload/ProjectBillInfoUpload",
	"api/PreFinalAccountFile/GetMyUploadFile",
	"api/PreFinalAccountFile/Add"
];

export function request (url, method, data) {
	var headers = {};
	// 判断token是否存在
	if (methodsToken.indexOf(url) >= 0) {
		// 获取用户token
		let userToken = auth.getToken();
		if (!userToken) {
			helper.jumpToLogin();
			return false;
		} 
		headers['Authorization'] = 'Bearer ' + userToken;
	}
	headers['Accept'] = 'application/json';
	headers['content-type'] = 'application/json';
	return new Promise((resolve, reject) => {
		uni.request({
			method: method,
			url: apiBaseUrl + url,
			data: data,
			header: headers,
			sslVerify: false,
			dataType: 'json',         
		}).then((response) => {
			console.log("接口请求数据");
			console.log(response);
			var errData = response[0];
			if(errData) {
				reject('网络连接不可用，请检查网络是否正常');
				// reject('操作失败，' + errData.errMsg);
				return false;
			}
			var resp = response[1];
			if(resp.statusCode === 401) {
				helper.jumpToLogin();
				return false;
			}
			if(resp.statusCode !== 200) {
				reject('操作失败，' + resp.errMsg);
				return false;
			}
			var respData = resp.data;
			if(respData.code !== 20000)
			{
				reject('操作失败，' + respData.message);
				return false;
			}
			resolve(respData);
			return true;
		}).catch(error => {
			reject(error)
		})
	});
}

// 上传图片
export function uploadImage (url, imageUrl) {
	var headers = {};
	// 判断token是否存在
	if (methodsToken.indexOf(url) >= 0) {
		// 获取用户token
		let userToken = auth.getToken();
		if (!userToken) {
			helper.jumpToLogin();
			return false;
		} 
		headers['Authorization'] = 'Bearer ' + userToken;
	}
	headers['Accept'] = 'application/json';
	headers['Content-Type'] = 'multipart/form-data';
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: apiBaseUrl + url,
			filePath: imageUrl,
			fileType: 'image',
			name: 'file',
			headers: headers,
			sslVerify: false,
			formData: {
				'method': 'images.upload',
				'upfile': imageUrl
			}
		}).then(response => {
			var errData = response[0];
			if(errData) {
				reject('操作失败，' + errData.errMsg);
				return false;
			}
			var resp = response[1];
			if(resp.statusCode === 401) {
				helper.jumpToLogin();
				return false;
			}
			if(resp.statusCode !== 200) {
				reject('操作失败，' + resp.errMsg);
				return false;
			}
			var respData = JSON.parse(resp.data);
			if(respData.code !== 20000)
			{
				reject('操作失败，' + respData.message);
				return false;
			}
			resolve(respData);
			return true;
		}).catch(error => {
			reject(error)
		})
	})
}
